package com.ticket.sass.admin.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ticket.sass.admin.mapper.FilmLikeMapper;
import com.ticket.sass.admin.service.BaseService;
import com.ticket.sass.common.entity.tenant.FilmLikeEntity;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ywb
 * @since 2024-03-07
 */
@Service
public class FilmLikeServiceImpl extends ServiceImpl<FilmLikeMapper, FilmLikeEntity> implements BaseService<FilmLikeEntity> {
    public Map<Long, Integer> getCountByFilmIds(List<Long> filmIds) {
        if (filmIds.size() == 0){
            return new HashMap<>();
        }
       
        QueryWrapper<FilmLikeEntity> wrapper = new QueryWrapper<>();
        wrapper.in("film_id", filmIds);
        wrapper.select("film_id", "count(*) as like_count");
        wrapper.groupBy("film_id");
        
        // 查询并返回结果
        List<Map<String, Object>> maps = baseMapper.selectMaps(wrapper);
        
        // 构建 Map<Long, Integer>
        Map<Long, Integer> countMap = new HashMap<>();
        for (Map<String, Object> map : maps) {
            Long filmId = (Long) map.get("film_id");
            Integer likeCount = ((Long) map.get("like_count")).intValue();
            countMap.put(filmId, likeCount);
        }
        
        return countMap;
    }
}
